package xworker.gluon.glisten.control;

import com.gluonhq.charm.glisten.control.Dialog;
import javafx.scene.Node;
import javafx.scene.control.ButtonBase;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.util.JavaFXUtils;

public class DialogActions {
    static{
        PropertyFactory.regist(Dialog.class, "contentTextProperty", o -> {
            Dialog<?> obj = (Dialog<?>) o;
            return obj.contentTextProperty();
        });
        PropertyFactory.regist(Dialog.class, "onCloseRequestProperty", o -> {
            Dialog<?> obj = (Dialog<?>) o;
            return obj.onCloseRequestProperty();
        });
        PropertyFactory.regist(Dialog.class, "contentProperty", o -> {
            Dialog<?> obj = (Dialog<?>) o;
            return obj.contentProperty();
        });
        PropertyFactory.regist(Dialog.class, "onShownProperty", o -> {
            Dialog<?> obj = (Dialog<?>) o;
            return obj.onShownProperty();
        });
        PropertyFactory.regist(Dialog.class, "onShowingProperty", o -> {
            Dialog<?> obj = (Dialog<?>) o;
            return obj.onShowingProperty();
        });
        PropertyFactory.regist(Dialog.class, "onHiddenProperty", o -> {
            Dialog<?> obj = (Dialog<?>) o;
            return obj.onHiddenProperty();
        });
        PropertyFactory.regist(Dialog.class, "onHidingProperty", o -> {
            Dialog<?> obj = (Dialog<?>) o;
            return obj.onHidingProperty();
        });
        PropertyFactory.regist(Dialog.class, "titleProperty", o -> {
            Dialog<?> obj = (Dialog<?>) o;
            return obj.titleProperty();
        });
        PropertyFactory.regist(Dialog.class, "autoHideProperty", o -> {
            Dialog<?> obj = (Dialog<?>) o;
            return obj.autoHideProperty();
        });
        PropertyFactory.regist(Dialog.class, "idProperty", o -> {
            Dialog<?> obj = (Dialog<?>) o;
            return obj.idProperty();
        });
        PropertyFactory.regist(Dialog.class, "graphicProperty", o -> {
            Dialog<?> obj = (Dialog<?>) o;
            return obj.graphicProperty();
        });
        PropertyFactory.regist(Dialog.class, "titleTextProperty", o -> {
            Dialog<?> obj = (Dialog<?>) o;
            return obj.titleTextProperty();
        });
    }

    public static void init(Dialog<?> node, Thing thing, ActionContext actionContext) {
        if (thing.valueExists("autoHide")) {
            node.setAutoHide(thing.getBoolean("autoHide"));
        }

        Node content = JavaFXUtils.getObject(thing, "content", actionContext);
        if (content != null) {
            node.setContent(content);
        }

        String contentText = JavaFXUtils.getString(thing, "contentText", actionContext);
        if (contentText != null) {
            node.setContentText(contentText);
        }

        Node graphic = JavaFXUtils.getObject(thing, "graphic", actionContext);
        if (graphic != null) {
            node.setGraphic(graphic);
        }

        String id = JavaFXUtils.getString(thing, "id", actionContext);
        if (id != null) {
            node.setId(id);
        }

        Node title = JavaFXUtils.getObject(thing, "title", actionContext);
        if (title != null) {
            node.setTitle(title);
        }

        String titleText = JavaFXUtils.getString(thing, "titleText", actionContext);
        if (titleText != null) {
            node.setTitleText(titleText);
        }
    }

    public static Dialog<Object> create(ActionContext actionContext) {
        Thing self = actionContext.getObject("self");
        Dialog<Object> node = new Dialog<>(self.getBoolean("fullscreen"));
        init(node, self, actionContext);

        actionContext.peek().put("parent", node);
        for (Thing child : self.getChilds()) {
            child.doAction("create", actionContext);
        }

        actionContext.g().put(self.getMetadata().getName(), node);
        return node;
    }

    public static void createContent(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        Dialog<Object> parent = actionContext.getObject("parent");
        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof Node){
                parent.setContent((Node) obj);
                break;
            }
        }
    }

    public static void createGraphic(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        Dialog<Object> parent = actionContext.getObject("parent");
        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof Node){
                parent.setGraphic((Node) obj);
                break;
            }
        }
    }

    public static void createTitle(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        Dialog<Object> parent = actionContext.getObject("parent");
        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof Node){
                parent.setTitle((Node) obj);
                break;
            }
        }
    }

    public static void createButtons(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        Dialog<Object> parent = actionContext.getObject("parent");
        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof ButtonBase){
                parent.getButtons().add((ButtonBase) obj);
            }
        }
    }

}
